class Solution:
    def smallestRangeII(self, A: list, K: int) -> int:
        A.sort()
        minVal = A[0]
        maxVal = A[-1]
        res = maxVal - minVal
        for i in range(len(A) - 1):
            a = A[i]
            b = A[i + 1]
            res = min(res, max(maxVal - K, a + K) - min(minVal + K, b - K))
        return res
